package com.hontech.bv.ui.fragment.ice

import android.view.View
import com.hontech.bv.R
import com.hontech.bv.conn.Device
import com.hontech.bv.conn.api.Ice
import com.hontech.bv.model.ice.IceStatus
import com.hontech.bv.task.appScope
import com.hontech.bv.ui.view.NumberSensorView
import com.hontech.bv.ui.view.SwitchCtrlView
import com.hontech.bv.ui.view.SwitchSensorView
import com.hontech.bv.ui.window.showProg
import kotlinx.coroutines.launch

class StatusDelegate (view: View) {

    private val swIceFull = view.findViewById<SwitchSensorView>(R.id.id_ice_sw_make_ice_full)
    private val swBucketHigh = view.findViewById<SwitchSensorView>(R.id.id_ice_sw_bucket_high)
    private val swBucketLow = view.findViewById<SwitchSensorView>(R.id.id_ice_sw_bucket_low)
    private val swLeak = view.findViewById<SwitchSensorView>(R.id.id_ice_sw_leak)

    private val numEnv = view.findViewById<NumberSensorView>(R.id.id_ice_nsv_env)
    private val numEvap = view.findViewById<NumberSensorView>(R.id.id_ice_nsv_evap)
    private val numSpeed = view.findViewById<NumberSensorView>(R.id.id_ice_nsv_stir_speed)
    private val swCompr = view.findViewById<SwitchSensorView>(R.id.id_ice_sw_compr)

    private val scvFan = view.findViewById<SwitchCtrlView>(R.id.id_ice_scv_fan)
    private val scvDropIce = view.findViewById<SwitchCtrlView>(R.id.id_ice_scv_drop_ice)
    private val scvValve = view.findViewById<SwitchCtrlView>(R.id.id_ice_scv_valve)
    private val scvPump = view.findViewById<SwitchCtrlView>(R.id.id_ice_scv_pump)
    private val scvStir = view.findViewById<SwitchCtrlView>(R.id.id_ice_scv_stir_motor)

    private val nsvDrawState = view.findViewById<NumberSensorView>(R.id.id_ice_nsv_draw_state)
    private val nsvMakeState = view.findViewById<NumberSensorView>(R.id.id_ice_nsv_make_state)

    private fun nameType(type: Int): String {
        return when(type) {
            1 -> "风扇"
            2 -> "排水阀"
            3 -> "水泵"
            4 -> "出冰继电器"
            5 -> "搅拌电机"
            else -> "未知"
        }
    }

    private fun nameValue(value: Int): String {
        return when (value) {
            0 -> "打开"
            1 -> "关闭"
            else -> "未知"
        }
    }

    init {
        scvFan.clickCb = {
            click(1, it)
        }
        scvValve.clickCb = {
            click(2, it)
        }
        scvPump.clickCb = {
            click(3, it)
        }
        scvDropIce.clickCb = {
            click(4, it)
        }
        scvStir.clickCb = {
            click(5, it)
        }
    }

    private fun click(type: Int, it: Boolean) {
        val value = if (it) 0 else 1
        appScope.launch {
            val prog = showProg("测试${nameType(type)}", "${nameValue(value)}")
            try {
                Ice.testParts(type, value)
                prog.success("成功")
            } catch (e: Exception) {
                e.printStackTrace()
                prog.error(e)
            }
        }
    }

    fun update() {
        val status = IceStatus.status
        val sw = status.switchValue()
        val ctrl = status.ctrlValue()

        swIceFull.set(sw.isIceFull)
        swBucketHigh.set(sw.isBucketHigh)
        swBucketLow.set(sw.isBucketLow)
        swLeak.set(sw.isLeak)

        nsvDrawState.set(status.drawStateString())
        nsvMakeState.set(status.makeStateString())

        if (sw.isEnvOk) {
            numEnv.set(status.envTemp.signedValue())
        } else {
            numEnv.set("异常")
        }

        if (sw.isEvapOk) {
            numEvap.set(status.evapTemp.signedValue())
        } else {
            numEvap.set("异常")
        }

        numSpeed.set(status.speed.value)

        swCompr.set(ctrl.isCompOpen)

        scvFan.set(ctrl.isFanOpen)
        scvDropIce.set(ctrl.isDropIceOpen)
        scvValve.set(ctrl.isValveOpen)
        scvPump.set(ctrl.isPumpOpen)
        scvStir.set(ctrl.isStirMotorOpen)

    }
}